package zct.proj.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

public class CommUtil {

	private final static String ZCT_PATH = "/zct";
	
	private static float brightness = 0f;
	public static void setBrightness(float value) {
		brightness = value;
	}
	public static float getBrightness() {
		return brightness;
	}
	
	public static void writeLog(String msg) {
//		try { 
////            File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Contacts.vcf");
//            File.createTempFile("dat", "log");
//
//         } catch (IOException ioe) {
//        	 ioe.printStackTrace();
//         }
		
		File sddir = Environment.getExternalStorageDirectory();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String today = sdf.format(new Date());
		try {
			String path = sddir.getCanonicalPath() + ZCT_PATH + "/" + today + ".log";
			File file = new File(path);
			if (!file.isFile()) {
				file.createNewFile();
			}
			

			
			OutputStreamWriter os = null;
			os = new OutputStreamWriter(new FileOutputStream(file, true), "GBK");
			sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			os.append(sdf.format(new Date()) + " | " + msg + "\r\n");
//			os.flush();
			os.close();
//			String content = sdf.format(new Date()) + " | " + msg;
//			FileWriter writer = new FileWriter(path, true);  
//			writer.write();
//			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
	}
	
	public static ArrayList<String> getStations() {
		File sddir = Environment.getExternalStorageDirectory();
		String path = null;
		ArrayList<String> sList = new ArrayList<String>();
		try {
			path = sddir.getCanonicalPath() + ZCT_PATH + "/stations";
			File f = new File(path);
			if (f.exists() && f.isDirectory()) {
				File[] fs = f.listFiles();
				for (File s : fs) {
					if (s.isDirectory()) {
						sList.add(s.getName());
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sList;
	}
	
	public static String getSoundFileFromSDCard(String stationName, String fileName) {
		File sddir = Environment.getExternalStorageDirectory();
		String path = null;
		try {
			path = sddir.getCanonicalPath() + ZCT_PATH + "/" + stationName + "/sound/" + fileName + ".wav";
			File f = new File(path);
			if (!f.exists()) {
				path = sddir.getCanonicalPath() + ZCT_PATH + "/common/sound/" + fileName + ".wav";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return path;
	}
	
	public static String getIniFileFromSDCard(String stationName) {
		File sddir = Environment.getExternalStorageDirectory();
		String path = null;
		try {
			path = sddir.getCanonicalPath() + ZCT_PATH + "/" + stationName + "/ini/" + stationName + ".ini";
		} catch (IOException e) {
			e.printStackTrace();
		}
		return path;
	}
	
	public static String getSwitcherFileFromSDCard(String stationName) {
		File sddir = Environment.getExternalStorageDirectory();
		String path = null;
		try {
			path = sddir.getCanonicalPath() + ZCT_PATH + "/" + stationName + "/ini/jkxx.ini";
		} catch (IOException e) {
			e.printStackTrace();
		}
		return path;
	}
	
    /** 
     * 注释：short到字节数组的转换！ 
     * 
     * @param s 
     * @return 
     */ 
    public static byte[] shortToByte(short number){ 
        int temp = number; 
        byte[] b = new byte[2]; 
        for(int i =0; i < b.length; i++){ 
            b[i] = Integer.valueOf(temp & 0xff).byteValue(); // 将最低位保存在最低位 
            temp = temp >>8;// 向右移8位 
        } 
        return b; 
    } 
 
    /** 
     * 注释：字节数组到short的转换！ 
     * 
     * @param b 
     * @return 
     */ 
    public static short byteToShort(byte[] b){ 
        short s =0; 
        short s0 =(short)(b[0] & 0xFF);// 最低位 
        short s1 =(short)(b[1] & 0xFF); 
        s1 <<=8; 
        s =(short)(s0 | s1); 
        return s; 
    }
    
    public static short byteToShort(byte b0, byte b1) {
        short s =0; 
        short s0 =(short)(b0 & 0xFF);// 最低位 
        short s1 =(short)(b1 & 0xFF); 
        s1 <<=8; 
        s =(short)(s0 | s1); 
        return s; 
    }
    
    public void getSoundFilePathWithName() {
    	
    }
    
	public static String getSetting(Context ctx, String key) {
       SharedPreferences settings = ctx.getSharedPreferences("ZctPref", 0);    
       return settings.getString(key, null);
	}
	
	public static void saveSetting(Context ctx, String key, String value) {
	    SharedPreferences settings = ctx.getSharedPreferences("ZctPref", 0);    
		SharedPreferences.Editor editor = settings.edit();
	    editor.putString(key, value);
	    editor.commit();
	}
}
